#define xsize 352 
#define ysize 288

  #pragma DATA_SECTION(linesegx, "SDRAME");
  int linesegx[20*200*2];//x and y positions of line segments
  #pragma DATA_SECTION(linesegy, "SDRAME");
  int linesegy[20*200*2];
  #pragma DATA_SECTION(segxinfo, "SDRAME");
  int segxinfo[20*7];// info corresponding to segments
  #pragma DATA_SECTION(segyinfo, "SDRAME");
  int segyinfo[20*7];//obj num,(x,y,angle)@start, '' at end
  #pragma DATA_SECTION(objx, "SDRAME");
  int objx[ysize*10*2];
  #pragma DATA_SECTION(objy, "SDRAME");
  int objy[xsize*10*2];
  #pragma DATA_SECTION(thingstatsx, "SDRAME");
  double thingstatsx[20*10];
  #pragma DATA_SECTION(thingstatsy, "SDRAME");
  double thingstatsy[20*10];
  #pragma DATA_SECTION(thingx, "SDRAMF");
  int thingx[20*ysize*2];
  #pragma DATA_SECTION(thingy, "SDRAMF");
  int thingy[20*xsize*2];
  #pragma DATA_SECTION(thingsplitx, "SDRAMF");
  int thingsplitx[20*ysize*2];
  #pragma DATA_SECTION(thingsplity, "SDRAMF");
  int thingsplity[20*xsize*2];
  #pragma DATA_SECTION(nodex, "SDRAMF");
  int nodex[10*4];//xpos,ypos,object split from, objnum
  #pragma DATA_SECTION(nodey, "SDRAMF");
  int nodey[10*4];
  #pragma DATA_SECTION(lines, "SDRAMF");
  unsigned * leds=(unsigned *)0x300000;

  double lines[5*6];


int getimdata(unsigned short* image,int index)
{
	int intnum=0;
	 int temp=index;
	while (temp>=16)
	{
	  intnum++;
	  temp-=16;
	 }
	 return ((*(image+intnum))>>(15-temp)&1);

}

int round(double input){
    double temp=input;
    while (input>1){
        input--;
    }
    if (input<.5){
        return (int)(temp-input);
    }
    else{
        return (int)(temp+1-input);
    }
}

void getxtarget(double *linereturn, int ytarget,int *prevseg,unsigned short *im)
{  



  //int ysize=292;
  //int xsize=356;
//  int objx[ysize*10*2];
// int objy[xsize*10*2];
//  double thingstatsx[20*10];
//  double thingstatsy[20*10];
  int objcountx=0;
  int objcounty=0;
  int yi=0; int xi=0;
  int mindist=5;
  int dist=mindist+1;
  int objfound=0; //bool
  int objnum=0;
  int center,length;
//  int thingx[20*ysize*2];
//  int thingy[20*xsize*2];
//  int thingsplitx[20*ysize*2];
//  int thingsplity[20*xsize*2];
  int thingcountx=0;
  int thingcounty=0;
  int thingfoundx=0;
  int thingfoundy=0;   //
  int cond1=0;        //
  int cond2=0;        //
  int cond3=0;        //bool
  int cond4=0;        //
  int previousfound=0;//
  int i;
  //int q;                         //NEVER USED
  //int j;                         //NEVER USED
  //int thingstarted;   //bool     NEVER USED
  //int lastx,lasty,xstart,ystart,width,elements,change;    NEVER USED
  int xpos;
  //int statswritten;   //bool      NEVER USED
  //int currwidth,prevwidth;        NEVER USED
  int start,end,prevstart,prevend;
  //int c1,c2,c3;                   NEVER USED
  //int ytarget;
  //double lines[5*6];
  //int linecount;                  NEVER USED
  //double disttemp;                NEVER USED
  //int linetemp;                   NEVER USED
  //double linereturn[5*4];
  //int currlineobj;                NEVER USED
//  int linesegx[20*200*2];//x and y positions of line segments
//  int linesegy[20*200*2];
//  int segxinfo[20*7];// info corresponding to segments
//  int segyinfo[20*7];//obj num,(x,y,angle)@start, '' at end
  int linesegcount;
//  int nodex[10*4];//xpos,ypos,object split from, objnum
//  int nodey[10*4];
  int nodecount;
  int xlinemin=5;
  int xlinemax=60;
  int ylinemin=3;
  int ylinemax=30;
  int linenow=0;
  int segindex;
  int ypos, prevxpos,prevypos;
  //int sum;                          NEVER USED
  int dx,dy;
  int minlinesize=11; //minimum length for line segments to be considered such
  int segwritten;
  int dx1,dy1,dx2,dy2;
  int intsegsx[3];//number to intersecting object 
  int intsegsy[3*2];//number of object, to left/right
  int numxints=0;
  int numyints=0;
  int currseg=-1;
  int tempdist;
  int distm=1000;
  int intxtotal=0;
  int intytotal=0;
  int numxsegs=0;
  int numysegs=0;


  // initialize matrixes to zero  
 
  for (i=0;i<=(ysize*10*2)-1;i++){
    objx[i]=0;
 }
 
 for (i=0;i<=(xsize*10*2)-1;i++){
     objy[i]=0;
 }

  for(i=0;i<=(20*ysize*2)-1;i++){
    thingx[i]=0;
    thingsplitx[i]=0;
  }
  
    for(i=0;i<=(20*xsize*2)-1;i++){
    thingy[i]=0;
    thingsplity[i]=0;
  }

  for(i=0;i<=(20*10)-1;i++){
      thingstatsx[i]=0;
  }

  for(i=0;i<=19;i++){
    thingstatsx[10*i+8]=-1;
  }
    //Stat not changed if unused, which is
    //possible even if the element (i) is used.
    //Set to -1 to avoid mistaking for the stat
    //being used and set to zero, since this is
    //also possible.
    for(i=0;i<=(20*10)-1;i++){
      thingstatsy[i]=0;
  }

  for(i=0;i<=19;i++){
    thingstatsy[10*i+8]=-1;}

  
  
  
  for(i=0;i<=(5*6)-1;i++){
    lines[i]=0;
  }

  for(i=0;i<=(5*4)-1;i++){
    linereturn[i]=0;
  }
  
  for(i=0;i<=20;i++){
    segxinfo[7*i+0]=-1;        //Arrays specify object numbers. Initialized to -1 to 
    segyinfo[7*i+0]=-1;        //aviod confusion with specifying object #0.
  }

//  printf("%s \n","starting...");


  //creates a matrix containing the first and last x
  //position ofevery grouping of ones in a single y
  //line repeated for every y line in the image
  //Format obj[yline][objnum][xstart/end]
  //For example: 11001110 gives:
  //obj[yi][0][0]=0; obj[yi][0][1]=1;
  //obj[yi][1][0]=4; obj[yi][1][1]=6;

  
  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  dist=mindist+1;
  for (yi=0;yi<=ysize-1;yi++){
    objcountx=0;
    for (xi=0;xi<=xsize-1;xi++){
      if (dist==mindist){
    objcountx=objcountx+1;
    objfound=0;
      }
      if (getimdata(im,ysize*xi+yi)==1){
    if (objfound==0){
      objx[10*2*yi+2*objcountx+0]=xi;
      objx[10*2*yi+2*objcountx+1]=xi;
      objfound=1;
      dist=0;
    }
    else{
      dist=0;
      objx[10*2*yi+2*objcountx+1]=xi;
    }
      }
      else{
    dist++;
      }
    }
  }



  //yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
  dist=mindist+1;
  for (xi=0;xi<=xsize-1;xi++){
    objcounty=0;
    for (yi=0;yi<=ysize-1;yi++){
      if (dist==mindist){
    objcounty=objcounty+1;
    objfound=0;
      }
      if (getimdata(im,ysize*xi+yi)==1){
    if (objfound==0){
      objy[10*2*xi+2*objcounty+0]=yi;
      objy[10*2*xi+2*objcounty+1]=yi;
      objfound=1;
      dist=0;
    }
    else{
      dist=0;
      objy[10*2*xi+2*objcounty+1]=yi;
    }
      }
      else{
    dist++;
      }
    }
  }
  
  //for (yi=0;yi<=ysize-1;yi++){               ???????????
  //  for(objcount=0;objcount<=9;objcount++){
  //    if (obj[10*2*yi+2*objcount+0]>0){
  //    }
  //  }
  //}                                          ???????????                                             
   // printf("%s \n","groups found...");
  

  


  //Starting from the bottom y position, groups
  //the sets on each y line together with the corresponding
  //sets on the other y lines that they are in contact with,
  //such that the result if a list of average x positions and
  //widths for each y line of each object in the image
  //Format: thing[object #][ypos][xpos/width]
  
  
  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 objcountx=0;
 for (objnum=0;objnum<=9;objnum++){
    if (objx[10*2*(ysize-1)+2*objnum+0]>0){
      thingx[ysize*2*objcountx+2*(ysize-1)+0]=objx[10*2*(ysize-1)+2*objnum+0];
      thingx[ysize*2*objcountx+2*(ysize-1)+1]=objx[10*2*(ysize-1)+2*objnum+1];
      objcountx++;
    }
  }
     
  nodecount=0;
  for (yi=ysize-2;yi>=0;yi--){
    for (objnum=0;objnum<=9;objnum++){
      previousfound=0;
      //printf("%s \n","a...");
        if (objx[10*2*yi+2*objnum+0]>0){
            thingfoundx=0;
            thingcountx=0;
                 // printf("%s \n","b...");
                  while(thingcountx<=19 && thingfoundx==0){
                start=objx[10*2*yi+2*objnum+0];
                end=objx[10*2*yi+2*objnum+1];
                prevstart=thingx[ysize*2*thingcountx+2*(yi+1)+0];
                prevend=thingx[ysize*2*thingcountx+2*(yi+1)+1];
                cond1=((!(start>=prevend))&&(!(end<=prevstart)));
                cond2=(thingfoundx==0);
                cond3=(previousfound==0);
                cond4=(thingsplitx[ysize*2*thingcountx+2*(yi+1)+0]==0);
                 //     printf("%s \n","c...");
                  if (cond1 && cond2 &&cond3){
                      if(cond4==1){//segment in previous position not already used
                        thingx[ysize*2*thingcountx+2*yi+0]=objx[10*2*yi+2*objnum+0];
                        thingx[ysize*2*thingcountx+2*yi+1]=objx[10*2*yi+2*objnum+1];
                        thingfoundx=1;
                        thingsplitx[ysize*2*thingcountx+2*(yi+1)+0]=1;
                        //      printf("%s \n","d...");
                    }
                    else{//previous segment used--indicate a split
                        //printf("%s \n","SPLIT");
                        thingsplitx[ysize*2*objcountx+2*yi+1]=1;
                        thingstatsx[10*objcountx+7]=1;
                        thingstatsx[10*objcountx+8]=(double)thingcountx;
                        thingstatsx[10*objcountx+9]=yi;
                        nodex[4*nodecount+0]=round(((double)(start+prevstart+end+prevend))/4);
                        nodex[4*nodecount+1]=yi;
                        nodex[4*nodecount+2]=thingcountx;
                        nodex[4*nodecount+3]=objcountx;
                          //    printf("%s \n","e...");
                    }
                  }
                  thingcountx++;
            }
            if ((thingfoundx==0) && cond3){    
                thingx[ysize*2*objcountx+2*yi+0]=objx[10*2*yi+2*objnum+0];
                thingx[ysize*2*objcountx+2*yi+1]=objx[10*2*yi+2*objnum+1];
                previousfound=1;
                objcountx++;
            }
      }    
     
    
  }
}

//yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
objcounty=0;
 for (objnum=0;objnum<=9;objnum++){
    if (objy[10*2*(xsize-1)+2*objnum+0]>0){
      thingy[xsize*2*objcounty+2*(xsize-1)+0]=objy[10*2*(xsize-1)+2*objnum+0];
      thingy[xsize*2*objcounty+2*(xsize-1)+1]=objy[10*2*(xsize-1)+2*objnum+1];
      objcounty++;
    }
  }

  nodecount=0;
  for (xi=ysize-2;xi>=0;xi--){
    for (objnum=0;objnum<=9;objnum++){
      previousfound=0;
        if (objy[10*2*xi+2*objnum+0]>0){
            thingfoundy=0;
            thingcounty=0;
                  while(thingcounty<=19 && thingfoundy==0){
                start=objy[10*2*xi+2*objnum+0];
                end=objy[10*2*xi+2*objnum+1];
                prevstart=thingy[xsize*2*thingcounty+2*(xi+1)+0];
                prevend=thingy[xsize*2*thingcounty+2*(xi+1)+1];
                cond1=((!(start>=prevend))&&(!(end<=prevstart)));
                cond2=(thingfoundy==0);
                cond3=(previousfound==0);
                cond4=(thingsplity[xsize*2*thingcounty+2*(xi+1)+0]==0);
                  if (cond1 && cond2 &&cond3){
                      if(cond4==1){//segment in previous position not already used
                        thingy[xsize*2*thingcounty+2*xi+0]=objy[10*2*xi+2*objnum+0];
                        thingy[xsize*2*thingcounty+2*xi+1]=objy[10*2*xi+2*objnum+1];
                        thingfoundy=1;
                        thingsplity[xsize*2*thingcounty+2*(xi+1)+0]=1;
                    }
                    else{//previous segment used--indicate a split
                        thingsplity[xsize*2*objcounty+2*xi+1]=1;
                        thingstatsy[10*objcounty+7]=1;
                        thingstatsy[10*objcounty+8]=(double)thingcounty;
                        thingstatsy[10*objcounty+9]=xi;
                        nodey[4*nodecount+0]=xi;
                        nodey[4*nodecount+1]=round(((double)(start+prevstart+end+prevend))/4);
                        nodey[4*nodecount+2]=thingcounty;
                        nodey[4*nodecount+3]=objcounty;
                    
                    }
                  }
                  thingcounty++;
            }
            if ((thingfoundy==0) && cond3){    
                thingy[xsize*2*objcounty+2*xi+0]=objy[10*2*xi+2*objnum+0];
                thingy[xsize*2*objcounty+2*xi+1]=objy[10*2*xi+2*objnum+1];
                previousfound=1;
                objcounty++;
            }
      }    
     
    
  }
}
  //printf("%s \n","objects grouped...");
  

  
  
    //From the matrix from the previous operation,
  //converts from first and last x position into
  //average x position and width
  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  for(thingcountx=0;thingcountx<=19;thingcountx++){
      for (yi=0;yi<=ysize-1;yi++){
      center=round(thingx[ysize*2*thingcountx+2*yi+0]+thingx[ysize*2*thingcountx+2*yi+1])/2;
      length=thingx[ysize*2*thingcountx+2*yi+1]-thingx[ysize*2*thingcountx+2*yi+0];
      thingx[ysize*2*thingcountx+2*yi+0]=center;
      thingx[ysize*2*thingcountx+2*yi+1]=length;
      if (objnum==0){
      }
    }
  }
 //yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 
  for(thingcounty=0;thingcounty<=19;thingcounty++){
      for (xi=0;xi<=xsize-1;xi++){
      center=round(thingy[xsize*2*thingcounty+2*xi+0]+thingy[xsize*2*thingcounty+2*xi+1])/2;
      length=thingy[xsize*2*thingcounty+2*xi+1]-thingy[xsize*2*thingcounty+2*xi+0];
      thingy[xsize*2*thingcounty+2*xi+0]=center;
      thingy[xsize*2*thingcounty+2*xi+1]=length;
      if (objnum==0){
      }
    }
  }
   
  //COdes compiles up to here
  
  //For each object checks at each point to see whether the width meets some min
  //and max constraint, and if so puts the part of the object that does into a new
  //array of possible line segments. For each such segment, puts the corresponding 
  //object number into seg(x or y)obj

  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  
  linesegcount=0;
  for (thingcountx=0;thingcountx<=19;thingcountx++){
          linenow=0;
          segindex=0;
      for (yi=0;yi<=ysize-1;yi++){
           segwritten=0; 
           xpos=thingx[ysize*2*thingcountx+2*yi+0]; 
           length=thingx[ysize*2*thingcountx+2*yi+1];
           if (length<=xlinemax && length>=xlinemin){
               if(linenow==1){//if line segment already started
                     linesegx[200*2*linesegcount+2*segindex+0]=xpos;//Write x &y positions
                	 linesegx[200*2*linesegcount+2*segindex+1]=yi;//to array
               }
               else{//if first element of line segment
                   linesegx[200*2*linesegcount+2*segindex+0]=xpos;
                   linesegx[200*2*linesegcount+2*segindex+1]=yi;
                   linenow=1;//indicates segment started
                   segxinfo[7*linesegcount]=thingcountx;//object number segment comes from
               	   segxinfo[7*linesegcount+1]=xpos;//starting x position
              	   segxinfo[7*linesegcount+2]=yi;//starting y position
	
               
               }
             if(yi==ysize-1){//
               if (segindex>=minlinesize){
                   segxinfo[7*linesegcount+4]=xpos;//last x position
                   segxinfo[7*linesegcount+5]=yi;//last y position
                   dx=prevxpos-thingx[ysize*2*thingcountx+2*(yi-10)+0];
                   dy=10;
            	   segxinfo[7*linesegcount+6]=90+(int)atan((double)dx/dy);//final angle
            	   segwritten=1;

                  }

             }
             if (segindex==10){
                 dy=10;
                 dx=xpos-thingx[ysize*2*thingcountx+2*(yi-10)+0];
                 segxinfo[7*linesegcount+3]=90+(int)atan(dx/dy);//initial angle

             }
           segindex++;    

           }
           else{
   
               if (linenow==1){//last index was the final index of a segment
                   
                    if(segindex>=minlinesize){
                     
                     segxinfo[7*(linesegcount)+4]=prevxpos;//final x position
                     segxinfo[7*(linesegcount)+5]=yi-1;//final y position
                     dx=prevxpos-thingx[ysize*2*thingcountx+2*(yi-11)+0];
                     dy=10;
                     segxinfo[7*(linesegcount)+6]=90+(int)atan((double)dx/dy);//final angle
                     segwritten=1;         
        
                 }
               }
               linenow=0;
             segindex=0;
               }

         prevxpos=xpos;
         if (segwritten==1){
		 	numxsegs++;	

             //printf("%s","Vertical Segment from object number ");
            // printf("%i \n",segxinfo[7*(linesegcount)+0]);
             //printf("%s","Starting x:");
             //printf("%i \n",segxinfo[7*(linesegcount)+1]);
             //printf("%s","Starting y:");
             //printf("%i \n",segxinfo[7*(linesegcount)+2]);
             //printf("%s","Starting angle:");
             //printf("%i \n",segxinfo[7*(linesegcount)+3]);
             //printf("%s","Final x:");
			 /*
             printf("%i \n",segxinfo[7*(linesegcount)+4]);
             printf("%s","Final y:");
             printf("%i \n",segxinfo[7*(linesegcount)+5]);
             printf("%s","Final angle:");
             printf("%i \n",segxinfo[7*(linesegcount)+6]);
              */
             linesegcount++; 
             
         }
           }
    }


  //yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
  linesegcount=0;
  for (thingcounty=0;thingcounty<=19;thingcounty++){
          linenow=0;
          segindex=0;
      for (xi=0;xi<=xsize-1;xi++){
         segwritten=0; 
         ypos=thingy[xsize*2*thingcounty+2*xi+0]; 
           length=thingy[xsize*2*thingcounty+2*xi+1];
           if (length<=ylinemax && length>=ylinemin){
                 if(linenow==1){//if line segment already started
                     linesegy[200*2*linesegcount+2*segindex+0]=xi;//Write x &y positions
                 	 linesegy[200*2*linesegcount+2*segindex+1]=ypos;//to array
                    }
               else{//if first element of line segment
                     linesegy[200*2*linesegcount+2*segindex+0]=xi;
                     linesegy[200*2*linesegcount+2*segindex+1]=ypos;
                     linenow=1;//indicates segment started
                     segyinfo[7*linesegcount]=thingcounty;//object number segment comes from
                     segyinfo[7*linesegcount+1]=xi;//starting x position
                     segyinfo[7*linesegcount+2]=ypos;//starting y position

               
               }
             if(xi==xsize-1){//
               if (segindex>=minlinesize){
                   segyinfo[7*linesegcount+4]=xi;//last x position
                   segyinfo[7*linesegcount+5]=ypos;//last y position
                   dx=10;
            dy=prevypos-thingy[xsize*2*thingcounty+2*(xi-10)+0];
          
            segyinfo[7*linesegcount+6]=(int)atan((double)dy/dx);//final angle
            segwritten=1;

                  }

             }
             if (segindex==10){
                     dx=10;
                 dy=ypos-thingy[xsize*2*thingcounty+2*(xi-10)+0];
                 segyinfo[7*linesegcount+3]=(int)atan((double)dy/dx);//initial angle
                 //    printf("%s","dy=");printf("%i \n",dy);
                 //    printf("%s","curr y=");printf("%i \n",ypos);
                 //    printf("%s","prev y=");printf("%i \n",thingy[xsize*2*thingcounty+2*(xi-10)+1]);

             }
           segindex++;    

           }
           else{
   
               if (linenow==1){//last index was the final index of a segment
                   
                    if(segindex>=minlinesize){
                     
                     segyinfo[7*(linesegcount)+4]=xi-1;//final x position
                     segyinfo[7*(linesegcount)+5]=prevypos;//final y position
                     dy=prevypos-thingy[xsize*2*thingcounty+2*(xi-11)+0];
                     dx=10;
                     segyinfo[7*(linesegcount)+6]=(int)atan((double)dy/dx);//final angle
   // printf("%s","dy=");printf("%i \n",dy);
                    segwritten=1;         
        
                 }
               }
               linenow=0;
             segindex=0;
               }

         prevypos=ypos;
         if (segwritten==1){
			numysegs++;
			/*
             printf("%s","Horizontal Segment from object number ");
             printf("%i \n",segyinfo[7*(linesegcount)+0]);
             printf("%s","Starting x:");
             printf("%i \n",segyinfo[7*(linesegcount)+1]);
             printf("%s","Starting y:");
             printf("%i \n",segyinfo[7*(linesegcount)+2]);
             printf("%s","Starting angle:");
             printf("%i \n",segyinfo[7*(linesegcount)+3]);
             printf("%s","Final x:");
             printf("%i \n",segyinfo[7*(linesegcount)+4]);
             printf("%s","Final y:");
             printf("%i \n",segyinfo[7*(linesegcount)+5]);
             printf("%s","Final angle:");
             printf("%i \n",segyinfo[7*(linesegcount)+6]);*/
             linesegcount++;
         }
           }
  }
   
   
    
  for(i=0;i<=20;i++){
    if(segxinfo[7*i]>=0){
      dx1=abs(*prevseg-segxinfo[7*i+1]);
      dy1=abs(*(prevseg+1)-segxinfo[7*i+2]);
      dx2=abs(*(prevseg+2)-segxinfo[7*i+4]);
      dy2=abs(*(prevseg+3)-segxinfo[7*i+5]);
      tempdist=sqrt(pow(dx1,2)+pow(dx2,2)+pow(dy1,2)+pow(dy2,2));
      if(tempdist<distm){
    distm=tempdist;
    currseg=i;
      }
    }
  }
  
  if(numxsegs==0){

    *(linereturn+2)=-1;//no vertical lines
	*leds=1;
  }
  else{
    *prevseg=segxinfo[7*currseg+1];
    *(prevseg+1)=segxinfo[7*currseg+2];
    *(prevseg+2)=segxinfo[7*currseg+4];
    *(prevseg+3)=segxinfo[7*currseg+5];
    if(segxinfo[7*currseg+5]>ytarget){
      *(linereturn+1)=thingx[ysize*2*segxinfo[7*currseg]+2*ytarget];
      *(linereturn+2)=ytarget;
    }
    else{
      *(linereturn+1)=segxinfo[7*currseg+4];
      *(linereturn+2)=segxinfo[7*currseg+5];
    }
    *(linereturn+3)=segxinfo[7*currseg+3];
    
  }
  for(i=0;i<20;i++){
    if(i!=currseg){
      if((segxinfo[7*i]>=0)&&(segxinfo[7*i+2]>=segxinfo[7*currseg+5]))
        dx1=abs(segxinfo[7*i+1]-segxinfo[7*currseg+4]);
        dy1=abs(segxinfo[7*i+2]-segxinfo[7*currseg+5]);
        if (sqrt(pow(dx1,2)+pow(dy1,2))<=20){
          intsegsx[numxints]=i;
          intxtotal+=segxinfo[7*i+1];
          intytotal+=segxinfo[7*i+2];
          numxints++;
        }
      }
    }
  for(i=0;i<20;i++){
    if(i!=currseg){
    dx1=abs(segyinfo[7*i+4]-segxinfo[7*currseg+4]);
        dy1=abs(segyinfo[7*i+5]-segxinfo[7*currseg+5]);
    dx2=abs(segyinfo[7*i+1]-segxinfo[7*currseg+4]);
        dy2=abs(segyinfo[7*i+2]-segxinfo[7*currseg+5]);
    if((sqrt(pow(dx1,2)+pow(dy1,2)))<=20){
      intsegsy[numyints]=i;
      intsegsy[numyints+1]=0;
      intxtotal+=segyinfo[7*i+1];
      intytotal+=segyinfo[7*i+2];
      numyints++;
    }
    else{
      if(sqrt(pow(dx2,2)+pow(dy2,2))<=20){
        intsegsy[numyints]=i;
        intsegsy[numyints]=1;
        intxtotal+=segyinfo[7*i+4];
        intytotal+=segyinfo[7*i+5];
        numyints++;
      }
    }
    }
  }
  intxtotal+=segxinfo[7*currseg+4];
  intytotal+=segyinfo[7*currseg+5];
  if(numxints==0 && numyints==0){
	*leds=2;
    *(linereturn+2)=0;//no intersections
  }
  if(numxints==0 && numyints==1){
    *(linereturn+2)=1+intsegsy[1];//left/right turn
  }
  if(numxints==1 && numyints==0){//some discontinuity in line
    *(linereturn+2)=3;
  }
  if((numxints+numyints)>1){
    *(linereturn+2)=4;//intersection
	*leds=4;
  }
  *(linereturn+4)=intxtotal/(1+numxints+numyints);
  *(linereturn+5)=intytotal/(1+numxints+numyints);

}
